package com.example.yls.broadcastreceiver;

import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
private Button btn1;
    private Button btn2;
private MyBroadcastReceiver receiver;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

       receiver = new MyBroadcastReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction("weather_info");
        filter.addAction("traffic_info");
/*
        filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
*/
        filter.addAction(Intent.ACTION_PACKAGE_ADDED);
        filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
        filter.addDataScheme("package");
        filter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
        registerReceiver(receiver,filter);

        btn1 = findViewById(R.id.btn_1);
        btn2 = findViewById(R.id.btn_2);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent1 = new Intent();
                intent1.setAction("weather_info");
                intent1.putExtra("weather","晴天");
                intent1.putExtra("warn",27);
                sendBroadcast(intent1);

            }
        });

        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent2 = new Intent();
                intent2.setAction("traffic_info");
                intent2.putExtra("traffic","畅通");
                intent2.putExtra("time",6);
                sendBroadcast(intent2);
            }

        });

    }
    protected void onDestroy(){
        unregisterReceiver(receiver);
        super.onDestroy();


    }
}
